/**
 * User interface to the Product controller.
 */

package TUILayer;
import java.util.Scanner;
import ModelLayer.*;
import ControlLayer.ProductCTR;


public class ProductUI
{
    private ProductCTR pCTR;
    /**
     * Constructor for objects of calss ProductUI
     */
    public ProductUI()
    {
        pCTR = new ProductCTR();
    }
    /**
     * Menu Section
     * 
     * start(), executes the menu.
     * productMenu(), is a selection method for the user menu.
     * writeProductMenu(), is a visual interpretation of the user menu for the product section.
     */
    public void start()
    {
        productMenu();
    }
    public void productMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeProductMenu();
            if(choise == 1)
            {
                findProduct();
            }
            else
            {
                if(choise == 2)
                {
                    createProduct();
                }
                else
                {
                    if(choise == 3)
                    {
                        createSpecimen();
                    }
                    else
                    {
                        if(choise == 4)
                        {
                            deleteProduct();
                        }
                        else
                        {
                            exit = true;
                        }
                    }
                }
            }
            
        }
        
    }
    private int writeProductMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show Product information");
            System.out.println(" (2) Create Product");
            System.out.println(" (3) Create Product specimen");
            System.out.println(" (4) Delete Product");
            System.out.println(" (5) Show current product list");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    
    /**
     * User input section
     * inputProductID(), inputs the products ID via user interaction.
     * inputProductName(), inputs the products name via user interaction.
     * inputProductDesc(), inputs the products description date via user interaction.
     * inputPrice(), inputs the products price via user interaction.
     * inputSalePrice(), inputs the products sale price date via user interaction.
     * inputQuantity(), inputs the products quantity via user interaction.
     * inputStock(), inputs the products stock information via user interaction.
     * inputRow(), inputs the products row information via user interaction.
     * inputShelf(), inputs the products shelf information via user interaction.
     * inputSerial(), inputs the products serial via user interaction.
     */
    private int inputProductID()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product id: ");
        int id = keyboard.nextInt();
        return id;
    }
    private String inputProductName()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product name: ");
        String name = keyboard.nextLine();
        return name;
    }
    private String inputProductDesc()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product description: ");
        String desc = keyboard.nextLine();
        return desc;
    }
    private double inputPrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product price: ");
        double price = keyboard.nextDouble();
        return price;
    }
    private double inputSalePrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product sale price: ");
        double saleprice = keyboard.nextDouble();
        return saleprice;
    }
    private int inputQuantity()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product quantity: ");
        int quantity = keyboard.nextInt();
        return quantity;
    }
    private int inputStock()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product stock: ");
        int stock = keyboard.nextInt();
        return stock;
    }
    private int inputRow()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product row: ");
        int row = keyboard.nextInt();
        return row;
    }
    private int inputShelf()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product shelf: ");
        int shelf = keyboard.nextInt();
        return shelf;
    }
    private int inputSerial()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product serialNo: ");
        int serial = keyboard.nextInt();
        return serial;
    }
    /**
     * User interaction section
     * findProduct(), finds product trough its ID via its perspectiv array.
     * createProduct(), creates product information and stores it in array.
     * createSpecimen(), creates specimen information and stores it in array.
     * deleteProduct(), deletes product if its ID is found in array.
     */
    private void findProduct()
    {
        int id = inputProductID();
        
        Product product = null;
        
        try 
        {
            if (product != null)
            {
                 System.out.println(" -- Product Information --");
                 System.out.println("");
                 System.out.println("Product name: " + product.getProductName());
                 System.out.println("Description: " + product.getdesc());
                 System.out.println("Price: " + product.getPrice());
                 System.out.println("Sales price: " + product.getSalePrice());
                 System.out.println("Quantity: " + product.getQuantity());
                 System.out.println("Stock: " + product.getStock());
                 System.out.println("Row: " + product.getRow());
                 System.out.println("Shelf: " + product.getShelf());
                 System.out.println("");
                 System.out.println("");
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            }
            else
            {
                System.out.println("\n ** No product with that id  was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
        
    }
    private void createProduct()
    {
        int id = inputProductID();
        String name = inputProductName();
        String desc = inputProductDesc();
        double price = inputPrice();
        double saleprice = inputSalePrice();
        int quantity = inputQuantity();
        int stock = inputStock();
        int row = inputRow();
        int shelf = inputShelf();
        pCTR.createProduct(id, name, desc, price, saleprice, quantity, stock, row, shelf);
    }
    private void createSpecimen()
    {
        int id = inputProductID();
        int serial = inputSerial();
        pCTR.createSpeciman(id, serial);
    }
    private void deleteProduct()
    {
        int id = inputProductID();
        
        try
        {
            if(pCTR.deleteProduct(id))
            {
                System.out.println("\n ** The Product was deleted! **");
                Thread.sleep(1500);
            }
            else
            {
                System.out.println("\n ** No Product with that ID was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    private int editProductData() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Edit Product name: ");
        System.out.println(" (2) Edit Product description: ");
        System.out.println(" (3) Edit Product price: ");
        System.out.println(" (4) Edit Product sale price: ");
        System.out.println(" (5) Edit Product quantity: ");
        System.out.println(" (6) Edit Product stock: ");
        System.out.println(" (7) Edit Product row: ");
        System.out.println(" (8) Edit Product shelf: ");
        int choise = keyboard.nextInt();
        return choise;
    }

//     private void startEditProduct() 
//     {
//         int id = inputProductID();
//         Product product = pCTR.findProduct(id);
//         
//         try
//         {
//             if (product != null)
//             {
//                 int nr = editDVDData();
//                 Scanner keyboardData = new Scanner(System.in); 
//                 System.out.print(" Enter new data: ");
//                 String newData = keyboardData.nextLine();
//                 dvdCtr.editDVDData(DVDobj, nr, newData);
//             }
//             else
//             {
//                 System.out.println("\n ** No DVD with that title was found! **");
//                 Thread.sleep(1500);
//             }
//         }
//         catch (Exception e)
//         {
//             System.out.println(" ** An error has occurred! **");
//         }
//     }

//     private void startEditProduct() 
//     {
//         int id = inputProductID();
//         Product product = pCTR.findProduct(id);
//         
//         try
//         {
//             if (product != null)
//             {
//                 int nr = editProductData();
//                 Scanner keyboardData = new Scanner(System.in); 
//                 System.out.print(" Enter new data: ");
//                 String newData = keyboardData.nextLine();
//                 int newInt = keyboardData.nextInt();
//                 double newDouble = keyboardData.nextDouble();
//                 ProductCTR.updateProduct(product, nr, newData, newInt, newDouble);
//             }
//             else
//             {
//                 System.out.println("\n ** No DVD with that title was found! **");
//                 Thread.sleep(1500);
//             }
//         }
//         catch (Exception e)
//         {
//             System.out.println(" ** An error has occurred! **");
//         }
//     }
}